package cn.lopr.im.android.mvp.view.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.ArticleVo;
import cn.lopr.im.android.bean.PageInfo;
import cn.lopr.im.android.utils.DateUtil;
import de.hdodenhof.circleimageview.CircleImageView;

public class CommunityArticleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_CONTENT = 0;
    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_END = 2;
    private final Context context;
    private CommunityArticleAdapter.OnRecyclerViewItemClickListener mOnItemClickListener;

    private PageInfo<ArticleVo> articleVos;

    private TextView tvLoadMore;
    private RelativeLayout rlLoad;
    private TextView tvOverLoad;
    private String currentPlateId;

    public CommunityArticleAdapter(Context context, PageInfo<ArticleVo> articleVos) {
        this.context = context;
        this.articleVos = articleVos;
    }

    public void refresh(PageInfo<ArticleVo> articleVos, String currentPlateId) {
        // 判断是否切换了板块， 如果切换了要清空数据
        if (articleVos.isIsFirstPage()) {
            this.articleVos.setList(articleVos.getList());
        } else if (TextUtils.isEmpty(this.currentPlateId) || this.currentPlateId.equals(currentPlateId)) {
            this.articleVos.getList().addAll(articleVos.getList());
        } else {
            this.articleVos.setList(articleVos.getList());
        }

        this.articleVos.setIsLastPage(articleVos.isIsLastPage());
        notifyDataSetChanged();
        this.currentPlateId = currentPlateId;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_CONTENT) {
            return new ContentViewHolder(LayoutInflater.from(parent.getContext()).inflate
                    (R.layout.list_item_community_article, parent, false));
        } else if (viewType == TYPE_FOOTER || viewType == TYPE_END) {
            // 加载进度条的布局
            return new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate
                    (R.layout.list_item_footer_load_more, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (type == TYPE_CONTENT) {
            initContent((ContentViewHolder) holder, position);

        } else if (type == TYPE_END) {
            tvOverLoad = ((FooterViewHolder) holder).tvOverLoad;
            rlLoad = ((FooterViewHolder) holder).rlLoad;
            tvLoadMore = ((FooterViewHolder) holder).tvLoadMore;
            tvOverLoad.setVisibility(View.VISIBLE);
            rlLoad.setVisibility(View.GONE);
            tvLoadMore.setVisibility(View.GONE);

        } else if (type == TYPE_FOOTER) {
            tvOverLoad = ((FooterViewHolder) holder).tvOverLoad;
            rlLoad = ((FooterViewHolder) holder).rlLoad;
            tvLoadMore = ((FooterViewHolder) holder).tvLoadMore;

        }
    }


    @SuppressLint("SetTextI18n")
    void initContent(ContentViewHolder holder, int position) {
        ArticleVo articleVo = this.articleVos.getList().get(position);
        if (mOnItemClickListener != null) {
            holder.llCommunityType.setTag(articleVo);
            holder.llCommunityType.setOnClickListener(v -> mOnItemClickListener.onItemClick(v, position));
            holder.llComment.setTag(articleVo);
            holder.llComment.setOnClickListener(v -> mOnItemClickListener.onItemClick(v, position));
            holder.ivPortrait.setTag(articleVo);
            holder.ivPortrait.setOnClickListener(v -> mOnItemClickListener.onItemClick(v, position));
            holder.llLike.setTag(articleVo);
            holder.llLike.setOnClickListener(v -> mOnItemClickListener.onItemClick(v, position));
            holder.ivMenu.setTag(articleVo);
            holder.ivMenu.setOnClickListener(v -> mOnItemClickListener.onItemClick(v, position));
        }

        if (articleVo.isSelf()) {
            holder.ivMenu.setVisibility(View.VISIBLE);
        } else {
            holder.ivMenu.setVisibility(View.GONE);
        }

        if (articleVo.isSelfLike()) {
            holder.ivLike.setImageResource(R.drawable.ic_yes_click);
        }

        if (articleVo.isSelfVisit()) {
            holder.ivVisit.setImageResource(R.drawable.ic_visit_click);
        }

        if (articleVo.isSelfComment()) {
            holder.ivComment.setImageResource(R.drawable.ic_comment_2_click);
        }

        if (articleVo.getVisits() == 0) {
            holder.tvVisit.setVisibility(View.GONE);
        } else {
            holder.tvVisit.setText(articleVo.getVisits().toString());
        }
        if (articleVo.getComment() == 0) {
            holder.tvComment.setVisibility(View.GONE);
        } else {
            holder.tvComment.setText(articleVo.getComment().toString());
        }
        if (articleVo.getLike() == 0) {
            holder.tvLike.setVisibility(View.GONE);
        } else {
            holder.tvLike.setText(articleVo.getLike().toString());
        }

        holder.tvContent.setText(articleVo.getContent());
        holder.tvPublishTime.setText(DateUtil.formatHMS(articleVo.getPublishTime()));
        holder.tvNickname.setText(articleVo.getNickname());

        Glide.with(context).load(articleVo.getPortrait()).placeholder(R.drawable.default_head_portrait)
                .dontAnimate()
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        holder.ivPortrait.setImageDrawable(resource);
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        holder.ivPortrait.setImageDrawable(errorDrawable);
                    }
                });
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int section);

        void onItemLongClick(View view, int section);
    }

    public void onRecyclerViewItemClickListener(CommunityArticleAdapter.OnRecyclerViewItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    /**
     * 获取数据集加上一个footer的数量
     */
    @Override
    public int getItemCount() {
        if (articleVos == null || articleVos.getList() == null) {
            return 0;
        }
        return articleVos.getList().size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            if (this.articleVos.isIsLastPage()) {
                return TYPE_END;
            }
            return TYPE_FOOTER;
        } else {
            return TYPE_CONTENT;
        }
    }

    /**
     * 获取数据集的大小
     */
    public int getListSize() {
        return articleVos.getList().size();
    }


    /**
     * 显示正在加载的进度条，滑动到底部时，调用该方法，上拉就显示进度条，隐藏"上拉加载更多"
     */
    public void showLoading() {
        if (this.articleVos.isIsLastPage()) {
            return;
        }

        if (rlLoad != null && tvLoadMore != null) {
            rlLoad.setVisibility(View.VISIBLE);
            tvLoadMore.setVisibility(View.GONE);
        }
    }

    /**
     * 显示上拉加载的文字，当数据加载完毕，调用该方法，隐藏进度条，显示“上拉加载更多”
     */
    public void showLoadMore() {
        if (this.articleVos.isIsLastPage()) {
            //tvOverLoad.setVisibility(View.VISIBLE);
            if (rlLoad != null && tvLoadMore != null) {
                rlLoad.setVisibility(View.GONE);
                tvLoadMore.setVisibility(View.GONE);
            }

            return;
        }
        if (rlLoad != null && tvLoadMore != null) {
            rlLoad.setVisibility(View.GONE);
            tvLoadMore.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 内容的ViewHolder
     */
    public static class ContentViewHolder extends RecyclerView.ViewHolder {
        public View mView;

        public LinearLayout llCommunityType;
        public LinearLayout llUserInfo;
        public LinearLayout llLike;
        public LinearLayout llComment;
        public LinearLayout llVisit;
        public CircleImageView ivPortrait;
        public TextView tvNickname;
        public TextView tvPublishTime;
        public TextView tvContent;
        public ImageView ivLike;
        public TextView tvLike;
        public ImageView ivComment;
        public TextView tvComment;
        public ImageView ivVisit;
        public TextView tvVisit;
        public ImageView ivMenu;

        public ContentViewHolder(View view) {
            super(view);
            this.mView = view;

            this.llCommunityType = (LinearLayout) view.findViewById(R.id.ll_community_type);
            this.llUserInfo = (LinearLayout) view.findViewById(R.id.ll_user_info);
            this.llLike = (LinearLayout) view.findViewById(R.id.ll_like);
            this.llComment = (LinearLayout) view.findViewById(R.id.ll_comment);
            this.llVisit = view.findViewById(R.id.ll_visit);
            this.ivPortrait = (CircleImageView) view.findViewById(R.id.iv_portrait);
            this.tvNickname = (TextView) view.findViewById(R.id.tv_nickname);
            this.tvPublishTime = (TextView) view.findViewById(R.id.tv_publish_time);
            this.tvContent = (TextView) view.findViewById(R.id.tv_content);
            this.ivLike = (ImageView) view.findViewById(R.id.iv_like);
            this.tvLike = (TextView) view.findViewById(R.id.tv_like);
            this.ivComment = (ImageView) view.findViewById(R.id.iv_comment);
            this.tvComment = (TextView) view.findViewById(R.id.tv_comment);
            this.ivVisit = (ImageView) view.findViewById(R.id.iv_visit);
            this.tvVisit = (TextView) view.findViewById(R.id.tv_visit);
            this.ivMenu = (ImageView) view.findViewById(R.id.iv_menu);
        }

    }

    /**
     * footer的ViewHolder
     */
    public static class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView tvLoadMore;
        TextView tvOverLoad;
        RelativeLayout rlLoad;

        public FooterViewHolder(View itemView) {
            super(itemView);
            tvLoadMore = (TextView) itemView.findViewById(R.id.tv_item_footer_load_more);
            tvOverLoad = (TextView) itemView.findViewById(R.id.tv_over_load);
            rlLoad = (RelativeLayout) itemView.findViewById(R.id.rl_load);
        }
    }

}